package learn.advance.thread1;

import javax.xml.transform.Source;

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        for (int i = 0; i < 2; i++) {
            System.out.println("run:" + i);
        }
    }
}

class Demo1Thread {
    public static void Test() {
        MyThread mt1 = new MyThread();
        MyThread mt2 = new MyThread();
        MyThread mt3 = new MyThread();
        mt1.start();
        mt2.start();
        mt3.start();
    }
}

class MyThread1 extends Thread {
    public MyThread1() {
    }

    public MyThread1(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

class Demo3SetThreadName {
    public static void Test() {
        MyThread1 mt1 = new MyThread1("t1");
        MyThread1 mt2 = new MyThread1("t2");
        MyThread1 mt3 = new MyThread1("t3");
        mt1.start();
        mt2.start();
        mt3.start();
    }
}

class Demo1Sleep {
    public static void Test() {

        try {
            Thread.sleep(2000);
            System.out.println("hello");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class RunableImpl implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(i + ":" + Thread.currentThread().getName());
        }
    }
}

class Demo01Runnable {
    public static void Test() {
        RunableImpl run = new RunableImpl();
        Thread t = new Thread(new RunableImpl());

        t.start();

        for (int i = 0; i < 20; i++) {
            System.out.println(i + ":" + Thread.currentThread().getName());
        }
    }
}


class Demo1InnerClassThread {

    public static void Test() {
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    System.out.println(i + ":" + Thread.currentThread().getName());
                }
            }
        }.start();

        Runnable r = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(i + ":" + Thread.currentThread().getName());
                }
            }
        };

        new Thread(r).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(i + ":" + Thread.currentThread().getName());
                }
            }
        }).start();
    }
}


public class TestThread {
    public static void main(String[] args) {
        //Demo1Thread.Test();
        //Demo3SetThreadName.Test();
        //Demo1Sleep.Test();
        //Demo01Runnable.Test();
        Demo1InnerClassThread.Test();
    }
}
